package com.lxk.mall.pms.convert.spu;//package com.lxk.mall.pms.convert.spu;

import com.lxk.mall.pms.domain.vo.spu.ProductSkuPropertyVO;
import com.lxk.mall.pms.domain.vo.spu.ProductSkuRespVO;
import com.lxk.mall.pms.domain.bo.spu.ProductSpuPageReqBO;
import com.lxk.mall.pms.domain.vo.spu.ProductSpuRespVO;
import com.lxk.mall.pms.controller.app.vo.AppProductSpuPageReqVO;
import com.lxk.mall.pms.domain.PmsProductSku;
import com.lxk.mall.pms.domain.PmsProductSpu;
import org.dromara.common.core.utils.BeanCopyUtils;
import org.dromara.common.core.utils.CollectionUtils;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.json.utils.JsonUtils;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Map;


/**
 * 商品 SPU Convert
 *
 * @author 芋道源码
 */
@Mapper
public interface ProductSpuConvert {

    ProductSpuConvert INSTANCE = Mappers.getMapper(ProductSpuConvert.class);

//    ProductSpuPageReqBO convert(AppProductSpuPageReqVO bean);

    default ProductSpuRespVO buildSpuRespVO(PmsProductSpu spu, List<PmsProductSku> skus) {
        ProductSpuRespVO spuVO = MapstructUtils.convert(spu, ProductSpuRespVO.class);
        if(spuVO != null) {
            spuVO.setSkus(MapstructUtils.convert(skus, ProductSkuRespVO.class, (source, desc) -> {
                desc.setPropertyList(JsonUtils.parseArray(source.getProperties(), ProductSkuPropertyVO.class));
            }));
        }
        return spuVO;
    }

    default List<ProductSpuRespVO> convertForSpuDetailRespListVO(List<PmsProductSpu> spus, List<PmsProductSku> skus) {
        Map<Long, List<PmsProductSku>> skuMultiMap = CollectionUtils.convertMultiMap(skus, PmsProductSku::getSpuId);
        return CollectionUtils.convertList(spus, spu -> buildSpuRespVO(spu, skuMultiMap.get(spu.getId())));
    }

}
